var MJModel           = require('./MJModel');
var MJProto           = require('./MJProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,

	onLoad: function () {
		this.msgbg = cc.find('msgbg', this.node);
		this.headSprite = cc.find('head', this.node).getComponent(cc.Sprite);
		this.nameLabel = cc.find('name', this.node).getComponent(cc.Label);
		this.idLabel = cc.find('id', this.node).getComponent(cc.Label);
		this.bankerNode = cc.find('banker', this.node);
		this.winScoreNode = this.node.getChildByName('winscore');
		this.loseScoreNode = this.node.getChildByName('losescore');
		this.light = this.node.getChildByName('light');
		this.readyNode = this.node.getChildByName('ready');
		this.fangzhuNode = this.node.getChildByName('fangzhu');
		this.node.active = false;

		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
	},

	setIndex: function (index) {
		this.index = index;
	},

	getChairID: function () {
		if (this.index == undefined) {
			return 4;
		}
		else {
			if (MJModel.getChairCount() != 2) {
				return (MJModel.getMyChairID()+this.index)%4;
			}
			else {
				if (this.index == 0) {
					return MJModel.getMyChairID();
				}
				else {
					return (MJModel.getMyChairID()+1)%2;
				}
			}
		}
	},

	gameInit () {
		let user = MJModel.getUserByChairID(this.getChairID());
		if (!user) {
			this.node.active = false;
			return;
		}
		this.node.active = true;
		this.nameLabel.string = user.userInfo.nickname;
		this.idLabel.string = ""+Math.floor(user.userInfo.score);
		let is_creator = (MJModel.getRoomCreatorChairID() == this.getChairID());
		this.fangzhuNode.active = is_creator;
		Global.CCHelper.updateSpriteFrame(user.userInfo.avatar, this.headSprite);
		this.bankerNode.active = (this.getChairID() == MJModel.getBankerChairID());
		this.readyNode.active = false;
		if (MJModel.getGameStatus() == MJProto.gameStatus.NONE) {
			this.readyNode.active = (user.userStatus&RoomProto.userStatusEnum.READY);
		}
		cc.find('offline', this.node).active = (user.userStatus&RoomProto.userStatusEnum.OFFLINE) > 0;
		if (MJModel.getGameStatus() == MJProto.gameStatus.PLAYING) {
			if (MJModel.getCurChairID() == this.getChairID()) {
				cc.find('light', this.node).active = true;
			}
			else {
				cc.find('light', this.node).active = false;
			}
		}
		else {
			cc.find('light', this.node).active = false;
		}
	},


	messageCallbackHandler: function(router, msg) {
		if(!MJModel.getGameInited()) { return; }
		if (this.getChairID() >= MJModel.getChairCount()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
			else if(msg.type === RoomProto.USER_READY_PUSH) {
				if (this.node.active && msg.data.chairID == this.getChairID()) {
					this.readyNode.active = true;
				}
			}
			else if(msg.type === RoomProto.OTHER_USER_ENTRY_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.gameInit();
				}
			}
			else if(msg.type === RoomProto.USER_LEAVE_ROOM_PUSH) {
				if (msg.data.roomUserInfo.chairID == this.getChairID()) {
					this.node.active = false;
				}
			}
			else if(msg.type === RoomProto.ROOM_USER_INFO_CHANGE_PUSH) {
				let user = MJModel.getUserByChairID(this.getChairID());
				if (this.node.active && user) {
					this.idLabel.string = MJModel.getCurScore(this.getChairID());
				}
			}
			else if(msg.type === RoomProto.USER_OFF_LINE_PUSH) {
				if (msg.data.chairID == this.getChairID()) {
					cc.find('offline', this.node).active = true;
				}
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === MJProto.GAME_BANKER_PUSH) {
				this.setBanker();
			}
			else if(msg.type === MJProto.GAME_STATUS_PUSH) {
				if (msg.data.gameStatus != MJProto.gameStatus.NONE) {
					this.readyNode.active = false;
				}
				if (msg.data.gameStatus != MJProto.gameStatus.PLAYING) {
					cc.find('light', this.node).active = false;
				}
			}
			else if(msg.type === MJProto.GAME_CHAT_PUSH) {
				this.gameChat(msg.data);
			}
			else if(msg.type === MJProto.GAME_TURN_PUSH) {
				if (msg.data.chairID == this.getChairID()) {
					cc.find('light', this.node).active = true;
				}
				else {
					cc.find('light', this.node).active = false;
				}
			}
		} 
	},

	setBanker: function () {
		let is_banker = (MJModel.getBankerChairID() == this.getChairID());
		this.bankerNode.active = is_banker;
	},

	/*
	 * 游戏聊天
	 */
	gameChat: function (data) {
		let node = cc.find('text', this.node);
		let label = cc.find('Label', node);
		if (data.chairID == this.getChairID()) {
			let user = MJModel.getUserByChairID(this.getChairID());
			if (cc.find('emotion', node)) {
				cc.find('emotion', node).destroy();
			}
			if (data.type == 1) {
				label.active = false;
				let emotionNode = Global.CCHelper.getChatEmotionNode(data.msg, () => { node.active = false; });
				emotionNode.parent = node;
				emotionNode.setPosition(-70, 0);
				emotionNode.scaleX = node.scaleX;
				emotionNode.name = 'emotion';
				node.setContentSize(130, 110);
				node.active = true;
			}
			else if(data.type == 2) {
				let index = ['快点啊，等到花儿都谢了！', '不要吵了，不要吵了。专心玩游戏吧！', '怎么又断线了，网络怎么这么差啊！', '不要走，决战到天亮！', '底牌亮出来绝对吓死你！', '风水轮流转，底裤都输光！', '看我通杀全场，这些钱都是我的！', '不好意思啊，我要离开一会！'].indexOf(data.msg);
				Global.AudioManager.playSound('Game/ChatAudio/' + ((user.userInfo.sex == 0)? 'man/':'woman/') +index);
				label.active = true;
				label.getComponent(cc.Label).string = data.msg;
				this.scheduleOnce(() => {
					node.width = label.width+50;
					node.height = 58;
				}, 0);
				node.active = true;
				node.stopAllActions();
				node.runAction(cc.sequence(
					cc.delayTime(2),
					cc.callFunc(() => {
						node.active = false;
					})
				));
			}
		}
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
	},
});

